//
//  ViewController.m
//  Browser
//
//  Created by iD Student on 7/11/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize addressBar;
@synthesize webView;

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    NSString *address=@"http://www.Google.com";
    NSURL *url =[NSURL URLWithString:address];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [webView loadRequest:request];
    [addressBar setText:address];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)goAddress:(id)sender
{
    if([[[addressBar text] substringToIndex:7] isEqualToString:@"http://"])
    {
        NSURL *url =[NSURL URLWithString:[addressBar text]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        [webView loadRequest:request];
        [addressBar resignFirstResponder];
    }
    else if([[[addressBar text] substringToIndex:3] isEqualToString:@"www"])
    {
        NSURL *url =[NSURL URLWithString:[@"http://" stringByAppendingString: [addressBar text]]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        [webView loadRequest:request];
        [addressBar resignFirstResponder];
        
    }
    else
    {
        
        NSURL *url =[NSURL URLWithString:[@"http://" stringByAppendingString: [addressBar text]]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [webView loadRequest:request];
        [addressBar resignFirstResponder];
                    [addressBar resignFirstResponder];
        }
    
    }


-(IBAction)goBack:(id)sender
{
    [webView goBack];
}
-(IBAction)goForward:(id)sender
{
    [webView goForward];
}
-(BOOL)webView: (UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
   if(navigationType == UIWebViewNavigationTypeLinkClicked)
   {
       NSURL *url = [request URL];
       if([[url scheme] isEqualToString:@"http:"])
       {
           [addressBar setText:[url absoluteString]];
           [self goAddress:nil];
       }
       return NO;
   }
    return YES;
}
@end
